﻿'use strict';
app
    // Login Box controller 
    .controller('LoginController', [
        '$rootScope', '$scope', '$http', '$modal', '$state', '$localStorage', '$stateParams', 'commonFactory',
        function ($rootScope, $scope, $http, $modal, $state, $localStorage, $stateParams, commonFactory) {
            //console.log($localStorage.user)
            $localStorage.user = { UserId: 0 };
            $scope.processing = false;
            if ($localStorage.user.UserId > 0) {
                $state.go('app.dashboard');
            }
            $scope.title = "Login page",
            $scope.messeger = "",
            $scope.loginData = { username: "", password: "" },
            $scope.popupLogin = function (windowClass) {
                    $modal.open({
                        windowClass: windowClass,
                        templateUrl: "WarningModal",
                        controller: "LoginModalInstanceCtrl",
                        size: "sm",
                        resolve: {
                            messeger: function () {
                                return $scope.messeger;
                            },
                            title: function () {
                                return $scope.title;
                            }
                        }
                    })
                },
            $scope.doLogin = function (loginData) {                
                if (angular.isUndefined(loginData) || loginData.username == '' || loginData.password == '')
                {
                    $scope.title = 'Login';
                    if (angular.isUndefined(loginData)) {
                        $scope.messeger = 'Please enter Username or Password';
                    }
                    else if (loginData.username == '') {
                        $scope.messeger = 'Please enter Username';
                    }
                    else if (loginData.password == '') {
                        $scope.messeger = 'Please enter Password';
                    }
                    $scope.popupLogin('modal-message modal-warning');
                } else {
                    $scope.processing = true;
                    $http.post($rootScope.APIAccount, { username: loginData.username, password: loginData.password }).then(
                        function successCallback(resp) {
                            if (resp.data.UserName != null)
                            {
                                GetUser(resp.data.UserId);                            
                            } else {
                                $scope.messeger = 'Login was unsuccessful. Please correct the errors and try again',
                                $scope.title = 'Warning',
                                $scope.popupLogin('modal-message modal-warning');
                            }
                            $scope.processing = false;
                        }, function errorCallback(resp) {
                            $scope.processing = false;
                            //err.status will contain the status code                      
                            $scope.messeger = 'Login was unsuccessful. You are not authorized. Please speak to your administrator';
                            $scope.title = 'Warning';
                            $scope.popupLogin('modal-message modal-warning');
                        }
                  );
                }
            };
            function GetUser(UserId) {
                commonFactory.getUserById(UserId).then(function (response) {                    
                    $localStorage.user = response.data;
                    $rootScope.token = $localStorage.user.SessionId;
                    $state.go('app.dashboard');                
                }, function (error) {
                    $scope.processing = false;
                    $scope.status = 'Unable to load profile data: ' + error.message;
                });
            }
            //function checkTrucTiep(userID)
            //{
            //    commonFactory.checkTrucTiep(userID).then(function (response) {
            //        $localStorage.CheckCN_TrucTiep = response.data.returnValue;
            //    },function (error) {
            //        $scope.status = 'Unable to load profile data: ' + error.message;
            //    });
            //}
            //function checkTrucTiepN(userID) {
            //    commonFactory.checkTrucTiepN(userID).then(function (response) {
            //        $localStorage.Check_TrucTiep = response.data.returnValue;
            //    }, function (error) {
            //        $scope.status = 'Unable to load profile data: ' + error.message;
            //    });
            //}
        }           
    ])
// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.
app.controller('LoginModalInstanceCtrl', function ($scope, $modalInstance, messeger, title) {
    $scope.messeger = messeger;
    $scope.title = title;
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});

